//
//  NewsNRViewController.m
//  MaxMovie
//
//  Created by Maxdong on 14-12-24.
//  Copyright (c) 2014年 lanou3g.com 蓝欧科技. All rights reserved.
//

#import "NewsNRViewController.h"
#import "NetworkHandler.h"
#import "UIImageView+WebCache.h"
#import "NewModel.h"
#import "NewsData.h"

@interface NewsNRViewController ()<UIWebViewDelegate>

@property (nonatomic, retain) UIWebView *WebView;
@property (nonatomic, retain) NSMutableArray *reduceId;
@property (nonatomic, assign) NSInteger reduceState;// 决定状态

@property (nonatomic, retain) UIBarButtonItem *CollectButton;
@property (nonatomic, retain) UIBarButtonItem *ShareButton;

@end

@implementation NewsNRViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
        self.Myid = [NSString string];
        
        self.title = @"✨MaxMovie News✨";
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    // 打开数据库
    NewsData *newsData = [NewsData shareInstance];
    [newsData openDB];
    self.reduceId = [newsData selectAllNews];
    NewModel *newsModel = [[NewModel alloc] init];
    
    // 遍历数据库 如果是收藏过的就将reduceState变成1
    for (int i = 0; i < [self.reduceId count]; i++) {
        newsModel = [self.reduceId objectAtIndex:i];
        NSString *string = [NSString stringWithFormat:@"%@", self.MyidStr];
        NSString *string1 = [NSString stringWithFormat:@"%@", newsModel.Myid];
        
        if ([string isEqualToString:string1]) {
            self.reduceState = 1;
        }
    }
    
   
 
    
    //网络请求
    NSString *str = _MyidStr;
    [NetworkHandler connectioonNetGETDataWithURLString:str completion:^(id result) {
        self.getData = result;
        
        
        //webview！！！！！！！！！！！
        NSString *htmlStr = [self.getData objectForKey:@"wapUrl"];
        
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:htmlStr]];
        self.WebView = [[UIWebView alloc]initWithFrame:CGRectMake(0,0,self.view.frame.size.width,self.view.frame.size.height )];
        self.WebView.backgroundColor = [UIColor whiteColor];
        [self.WebView setScalesPageToFit:YES];
        _WebView.delegate = self;
        [self.WebView loadRequest:request];
        [self.view addSubview:self.WebView];
        [self.WebView release];
        
    }];
    
    
    
    // 将多个barButtonItem加添到数组中
    self.ShareButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"share.png"] style:UIBarButtonItemStyleDone target:self action:@selector(ShareButtonAction:)];
    
    self.CollectButton = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"shoucang.png"] style:UIBarButtonItemStyleDone target:self action:@selector(ColectButtonAction:)];
    
    
    // 根据reduceState来决定显示的是哪一个图片
    if (1 == self.reduceState) {
        self.CollectButton.image = [UIImage imageNamed:@"yishoucang.png"];
    } else {
        self.CollectButton.image = [UIImage imageNamed:@"shoucang.png"];
    }
    
    
    NSArray *array = [NSArray arrayWithObjects:self.ShareButton, self.CollectButton, nil];
    
    // 将数组里面的所有item显示到navigationBar上
    self.navigationItem.rightBarButtonItems = array;
}

- (void)ShareButtonAction:(UIButton *)button
{
    //    NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"ShareSDK"  ofType:@"jpg"];
    //    //构造分享内容
    //    id<ISSContent> publishContent = [ShareSDK content:self.webUrlStr
    //                                       defaultContent:@"默认分享内容，没内容时显示"
    //                                                image:[ShareSDK imageWithPath:imagePath]
    //                                                title:@"ShareSDK"
    //                                                  url:@"http://www.lanou3g.com"
    //                                          description:@"这是一条测试信息"
    //                                            mediaType:SSPublishContentMediaTypeNews];
    //
    //    [ShareSDK showShareActionSheet:nil
    //                         shareList:nil
    //                           content:publishContent
    //                     statusBarTips:YES
    //                       authOptions:nil
    //                      shareOptions: nil
    //                            result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
    //                                if (state == SSResponseStateSuccess)
    //                                {
    //                                }
    //                                else if (state == SSResponseStateFail)
    //                                {
    //                                }
    //                            }];
    
}

// 点击收藏的方法
- (void)ColectButtonAction:(UIButton *)button
{
    NewsData *newsData = [NewsData shareInstance];
    [newsData openDB];
    
    NewModel *newsModel = [[NewModel alloc] init];
    
    newsModel.title = self.titleStr;
    newsModel.summaryInfo = self.summaryInfoStr;
    newsModel.image = self.imageStr;
    NSString *str = [NSString stringWithFormat:@"%@", self.MyidStr];
    newsModel.Myid = str;
    if (self.reduceState == 1) {
        self.CollectButton.image = [UIImage imageNamed:@"shoucang.png"];
        self.reduceState = 0;
        [newsData deleteNews:newsModel];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"取消收藏" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
    } else {
        [newsData insertObjectToNews:newsModel];
        self.CollectButton.image = [UIImage imageNamed:@"yishoucang.png"];
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alertView show];
        [alertView release];
        
    }
    
    
}


-(void)dealloc
{
    [_modelNameStr release];
    [_titleStr release];
    [_imageStr release];
    [_summaryInfoStr release];
    [_reduceId release];
    [_modelNameStr release];
    [_webURL release];
    [_Myid release];
    [_CollectButton release];
    [_ShareButton release];
    [super dealloc];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
